<?php
/*
	商品出库
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?:'list';
$tabs=array(
	array('op'=>'list','title'=>'选择仓库'),
);
if($_RQ['op']=='list'){
	//初始化返回前端的筛选条件数组
	$return['params']=array();
	//整理列表数据查询条件
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['title']){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$psize=15;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'id desc',
	);
	//执行分页查询
	$list=$this->get_list('depot',$parameter,$total);
	//将查询到的结果数组放入$return以备传回前端
	$return['list']=$list;
	//整理分页
	$return['totalpage'] =intval(ceil($total/$psize));
	$return['params']['page'] =$pindex;
	//声明列表页样式数组
	$htmls=array(
		'type'=>'list',
		'list'=>array('data'=>'list','del_one'=>false,'delall'=>false,'edit'=>false,'list'=>array(
			//列表展示列
			array('type'=>'string','name'=>'title','title'=>'仓库名称',),
			array('type'=>'string','name'=>'address','title'=>'仓库地址',),
		),'action'=>array(
			array('type'=>'href','op'=>'add','title'=>'出库商品'),
		)),
		'search'=>array(
			//列表查询表单
			array('type'=>'string','name'=>'title','title'=>'仓库名称','width'=>'50'),
		),
	);
	$return['htmls']=$htmls;
	//把数据返回前端
	exi($return);
}
if($_RQ['op']=='add'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$depot_id=$post['depot_id'];
		$data=array_fields(array('num','depot_id','remark'),$post);
		$id=intval($post['id']);
		if(!$id){
			exi('请先选择商品！','error');
		}
		if(!$data['num']){
			exi('请先设置出库数量！','error');
		}
		$depot_goods=$this->pdo_get('depot_goods',array('id'=>$id));
		if(!$depot_goods){
			exi('指定商品不存在！','error');
		}
		if($depot_goods['total']<$data['num']){
			exi('商品库存不足！','error');
		}
		$this->model('depot')->out_goods($data,$depot_goods);
		exi('出库成功~','','/depot.out_log/');
	}
	$depot_id=intval($_RQ['id']);
	if(!$depot_id){
		exi('请从仓库进入！','error');
	}
	$this->session('depot_id',$depot_id);
	$return['item']=array('depot_id'=>$depot_id);
	$return['htmls']=array(
		'type'=>'edit',
		'edit'=>array(
			array('type'=>'choose_up','name'=>'id','title'=>'出库商品ID','action'=>'/depot.goods/choose/'),
			array('type'=>'string','name'=>'title','title'=>'出库商品名称','read'=>true),
			array('type'=>'image','name'=>'image','title'=>'出库商品图片','read'=>true),
			array('type'=>'string','name'=>'total','title'=>'商品当前库存','read'=>true),
			array('type'=>'string','name'=>'num','title'=>'出库数量'),
			array('type'=>'text','name'=>'remark','title'=>'备注'),
		),
	);
	$return['depots']=$this->pdo_getall('depot',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	exi($return);
}